//
//  SLShipRangeCollectionViewCell.m
//  StarLight
//
//  Created by 吴涛 on 16/5/11.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLShipRangeCollectionViewCell.h"

@interface SLShipRangeCollectionViewCell ()

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIImageView *rightImage;

@end


@implementation SLShipRangeCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
  if (self) {
    self.isTaped = NO;
    
    [self addSubview:self.label];
    self.layer.borderWidth = 0.5f;
    
    @weakify(self)
    [RACObserve(self, isTaped) subscribeNext:^(NSNumber *select) {
      @strongify(self)
      
      if (select.boolValue) {
        [self wasSelected];
      }else{
        [self wasDeSelected];
      }
      [self layoutIfNeeded];
    }];
    
    
  }
  return self;
}

#pragma mark - Public Method

- (void)configShipRangeWithTitle:(NSString *)title{
  self.label.text = title;
}


#pragma mark - Private Method

- (void)wasSelected{
  
  [self addSubview:self.rightImage];
  [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(CGSizeMake(24, 24));
    make.right.equalTo(self);
    make.centerY.equalTo(self);
  }];
  [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.rightImage.mas_left).offset(-10);
    make.centerY.equalTo(self);
  }];
  self.label.textColor = [UIColor whiteColor];
  self.backgroundColor = SLYellowColor;
  self.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (void)wasDeSelected{
  [self.rightImage removeFromSuperview];
  [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self);
  }];
  
  self.label.textColor = SLBlackColor;
  self.backgroundColor = [UIColor whiteColor];
  self.layer.borderColor = [UIColor lightGrayColor].CGColor;
}



#pragma mark - getter


- (UILabel *)label{
  if (!_label) {
    _label = [UILabel labelWithFontSize:13 textColor:[UIColor whiteColor]];
  }
  return _label;
}


- (UIImageView *)rightImage{
  if (!_rightImage) {
    _rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Clean"]];
  }
  return _rightImage;
}

@end
